//#include "stdafx.h"
#include "stdafx.h"

#include "CImageControl.h"

#include <GdiPlus.h>

//Macro to release COM Components

#ifdef SAFE_RELEASE
#undef SAFE_RELEASE
#endif
#define SAFE_RELEASE(x) do{\
	if((x) != NULL)\
{\
	while((x)->Release() != 0);\
	(x) = NULL;\
}\
}while(0)

//#include "global.h"
using namespace Gdiplus;

extern CString g_strBigImageName;

BEGIN_MESSAGE_MAP(CImageControl, CStatic)
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

CImageControl::CImageControl(void)
{
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
	m_pStream = NULL;
	m_bIsPicLoaded = FALSE;

	m_strImageName =L"";
	m_nImageHeight = 0;		// Image Height
	m_nImageWidth  = 0;		// Image Width
	m_nID          = 0;		// ID of control
}

void CImageControl::SetImageHeight( int ImageHeight )
{
	m_nImageHeight = ImageHeight;
}
void CImageControl::SetImageWidth( int ImageWidth )
{
	m_nImageWidth = ImageWidth;
}
int  CImageControl::GetImageHeight()
{
	return m_nImageHeight;
}
int  CImageControl::GetImageWidth()
{
	return m_nImageWidth;
}

CImageControl::~CImageControl(void)
{
	GdiplusShutdown(m_gdiplusToken);
}
BOOL CImageControl::CreateImageFromFile(CString &szFilePath)
{
	////Set success error state
	//SetLastError(ERROR_SUCCESS);
	//FreeData();

	//Allocate stream
	DWORD dwResult = CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
	if(dwResult != S_OK)
	{
		SetLastError(dwResult);
		return FALSE;
	}

	//Open the specified file
	CFile cFile;
	CFileException cFileException;
	if(!cFile.Open(szFilePath, CStdioFile::modeRead | CStdioFile::typeBinary, &cFileException))
	{
		SetLastError(cFileException.m_lOsError);
		SAFE_RELEASE(m_pStream);
		return FALSE;
	}
	// Save the path name
	m_strImageName = szFilePath;

	//Copy the specified file's content to the stream
	BYTE pBuffer[1024] = {0};
	while(UINT dwRead = cFile.Read(pBuffer, 1024))
	{
		dwResult = m_pStream->Write(pBuffer, dwRead, NULL);
		if(dwResult != S_OK)
		{
			SetLastError(dwResult);
			SAFE_RELEASE(m_pStream);
			cFile.Close();
			return FALSE;
		}
	}

	//Close the file
	cFile.Close();

	//Mark as Loaded
	m_bIsPicLoaded = TRUE;

	Invalidate();
	RedrawWindow();

	return TRUE;
}

void CImageControl::PreSubclassWindow()
{
	CStatic::PreSubclassWindow();
	ModifyStyle(0, SS_OWNERDRAW);
}
void CImageControl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	//Check if pic data is loaded
	if(m_bIsPicLoaded)
	{

		//Get control measures
		RECT rc;
		this->GetClientRect(&rc);

		Graphics graphics(lpDrawItemStruct->hDC);
		Image image(m_pStream);
	//	graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right-rc.left), (INT)(rc.bottom-rc.top));
		graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)this->m_nImageWidth, (INT)this->m_nImageHeight);

	}
}

BOOL CImageControl::OnEraseBkgnd(CDC *pDC)
{
	if(m_bIsPicLoaded)
	{

		//Get control measures
		RECT rc;
		this->GetClientRect(&rc);

		Graphics graphics(pDC->GetSafeHdc());
		LARGE_INTEGER liSeekPos;
		liSeekPos.QuadPart = 0;
		m_pStream->Seek(liSeekPos, STREAM_SEEK_SET, NULL);
		Image image(m_pStream);
		//graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right-rc.left), (INT)(rc.bottom-rc.top));
		graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)this->m_nImageWidth, (INT)this->m_nImageHeight);

		return TRUE;
	}
	else
	{
		return CStatic::OnEraseBkgnd(pDC);
	}
}

void CImageControl::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWnd *hpParent = NULL;
	hpParent = GetParent();
	g_strBigImageName = m_strImageName;

	if ( hpParent != NULL )
	{
		CWnd *hpParentParent = NULL;
		hpParentParent = hpParent->GetParent();

		if ( hpParentParent != NULL )
		{
			hpParentParent->Invalidate( 1 );
		}
		//hpParent->Invalidate( 1 );
		//hpParent->rew
	}
	//::SendMessageA(
}
